# 搜索某指定字幕，并打印相关信息

import subprocess

def run_command_real_time(command):
    saveout = ''
    try:
        # 使用 Popen 执行命令
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        # 实时读取标准输出和标准错误
        while True:
            output = process.stdout.readline()  # 逐行读取输出
            if output == '' and process.poll() is not None:
                break
            if output:
                print(output.strip())  # 打印输出
                saveout += output.strip() + '\n'

        # 检查命令的返回值
        return_code = process.poll()
        if return_code != 0:
            error_output = process.stderr.read()
            print(f"命令执行失败，返回码: {return_code}")
            print(f"错误信息: {error_output.strip()}")

    except Exception as e:
        print(f"发生错误: {e}")
    return saveout


if __name__ == "__main__":

    command = (
    "yt-dlp --list-subs "
    "--proxy http://127.0.0.1:10809 "
    "--cookies D:\Downloads\www.youtube.com_cookies.txt "
    "https://www.youtube.com/watch?v=8EXrxGByJJg"
    )

    # --list-subs  列出每个视频的可用字幕。 Simulate unless --no-simulate is used

    
    res = run_command_real_time(command)
    reslines = res.split('\n')
    en = ''
    for i, v in enumerate(reslines):
        if 'English' in v:
            en = v
    print('-'*10)
    print(en)
